package com.wonderful.mall.seckill.controller;

import com.alibaba.fastjson.TypeReference;
import com.wonderful.common.utils.R;
import com.wonderful.mall.seckill.feign.CouponFeignService;
import com.wonderful.mall.seckill.service.SecKillService;
import com.wonderful.mall.seckill.to.SeckillSkuRedisTo;
import com.wonderful.mall.seckill.vo.SecKillInfoVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class SecKillController {
    @Resource
    SecKillService seckillService;

    @Resource
    CouponFeignService couponFeignService;

    /**
     * @return 返回当前时间可以参与秒杀的商品
     */
    @ResponseBody
    @GetMapping("/currentSecKillSku")
    public R getCurrentSecKillSku() {
//        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        R r = couponFeignService.getSecKillSku();
        if (r.getCode() == 0) {
            List<SecKillInfoVo> vos = r.getData(new TypeReference<List<SecKillInfoVo>>() {
            });
            return R.ok().setData(vos);
        }
        return R.error("获取商品失败");
    }
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R skuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSecKillInfo(skuId);
        return R.ok().setData(to);
    }

    //kill?killId=2_15&num=1
    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId, @RequestParam("num") String num, Model model) {
        String orderSn = seckillService.kill(killId,num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
